package com.example.ythhome.login.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ythhome.login.entity.ResponseData;
import com.example.ythhome.login.entity.Role;
import com.example.ythhome.login.entity.RoleMenu;
import com.example.ythhome.login.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("api/roleMenu")
public class RoleMenuController {

    @Autowired
    RoleMenuService roleMenuService;

    @PostMapping("/save")
    @ResponseBody
    public ResponseData<Boolean> saveMenu(@RequestBody Map<String, Object> params) {
        Integer roleId = (Integer) params.get("roleId");
        List<RoleMenu> roleMenus = (List<RoleMenu>) params.get("roleMenus");
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        roleMenuService.remove(queryWrapper);
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, roleMenuService.saveBatch(roleMenus));
    }

    @PostMapping("/queryByRoleId")
    @ResponseBody
    public ResponseData<List> saveMenu(@RequestBody Integer roleId) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        List<Integer> list = new ArrayList<>();
        roleMenuService.list(queryWrapper).forEach(item->{
             list.add(item.getMenuId());
        });
        return new ResponseData<List>(ResponseData.StatusCodeEnum_SUCCESS, list);
    }
}
